<?php

namespace App\Console\Commands;

use Encore\Admin\Auth\Database\Menu;
use Encore\Admin\Auth\Database\Permission;
use Encore\Admin\Auth\Database\Role;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class rundb extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'run_db';
    
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = <<<DESC
一键部署
DESC;
    
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 更新菜单等，每次部署都刷新
        $this->adminMenus();
        
        $this->call('migrate', ['--force' => true]);
        
        $this->createPermissions();
    }
    
    private function adminMenus()
    {
        // add default menus.
        Menu::truncate();
        Menu::insert([
            'id' => 2,
            'parent_id' => 0,
            'order' => 2,
            'title' => '管理员管理',
            'icon' => 'fa-tasks',
            'uri' => '',
        ]);
        Menu::insert([
            [
                'parent_id' => 2,
                'order' => 3,
                'title' => '管理员列表',
                'icon' => 'fa-users',
                'uri' => 'auth/users',
            ],
        ]);
        DB::insert(
            'insert ignore into admin_menu ' .
            '( id, parent_id, `order`, title, icon, uri, created_at, updated_at ) ' .
            'values '
            . ' ( 100, 0,   100, "店铺管理", "fa-envelope", "", null, "")'
            . ',( 101, 100, 101, "店铺列表", "fa-bars", "' . no_admin_uri('shops.index') . '", null, "")'
            . ',( 120, 0,   120, "商品管理", "fa-envelope", "", null, "")'
            . ',( 121, 120, 121, "商品列表", "fa-bars", "' . no_admin_uri('goods.index') . '", null, "")'
        );
    }
    
    private function createPermissions()
    {
        Permission::truncate();
        Permission::insert([
            [
                'id' => 1,
                'name' => '所有权限',
                'slug' => '*',
                'http_method' => '',
                'http_path' => '*',
            ],
            [
                'id' => 2,
                'name' => '个人资料',
                'slug' => 'auth.setting',
                'http_method' => '',
                'http_path' => "/auth/setting\r\n/auth/logout/\r\n/api*\r\n/auth/google_2fa\r\n/wang_upload",
            ],
            [
                'id' => 10,
                'name' => '管理员列表',
                'slug' => 'auth.management',
                'http_method' => '',
                'http_path' => "/auth/users\r\n/auth/roles\r\n/auth/permissions\r\n/auth/menu",
            ],
        ]);
        // 添加通用的权限
        $sql1 = <<<SQL
INSERT INTO admin_permissions ( id, NAME, slug, http_path )
SELECT id, title, '', concat( uri, '*' )
FROM
	`admin_menu`
WHERE
	parent_id != 0
	AND id > 100
SQL;
        DB::statement($sql1);
        // 有特殊的可以再添加在这
        
        $hasAdminRole = Role::where('slug', 'administrator')->exists();
        if ($hasAdminRole) {
            $adminRoleHasPermission = Role::where('slug', 'administrator')->withCount('permissions')->value('permissions_count');
            if (!$adminRoleHasPermission) {
                // all permission to administrator initally
                Role::first()->permissions()->save(Permission::first());
            }
        }
    }
}
